//
//  LKSequenceLoadingAnimation.m
//  LKNovelty
//
//  Created by Pie on 17/3/31.
//  Copyright © 2017年 Laka. All rights reserved.
//

#import "LKSequenceAnimation.h"
#import "BNDOfficeDocKit.h"

@interface LKSequenceAnimation ()
@property (strong, nonatomic) YYAnimatedImageView *animatedImageView;
@property (strong, nonatomic) UIImageView         *staticStateImageView;
@end

@implementation LKSequenceAnimation

- (void)initImageViewUI:(UIView*)baseView
{
    CGSize imageSize = CGSizeMake(30, 30);
    NSString *imagePrefix = @"loading";
    
    if(_lineType == LKSequenceAnimationBig) {
        imagePrefix = @"loading";
        imageSize = CGSizeMake(52, 52);
    }
    
    NSMutableArray * imageArray = [NSMutableArray array];
    for (int i = 1; i < 20; i ++) {
        @autoreleasepool {
                NSString * str = [NSString stringWithFormat:@"%@%d",imagePrefix, i];
                UIImage * image = BNDImageNamed(str);
            NSLog(@"626262 %@",image);
                NSData *data = UIImagePNGRepresentation(image);
            NSLog(@"626263 %@",image);
            if(image == nil){
                NSLog(@"626264  %@",str);
            }
            if(image != nil){
                NSLog(@"626264  %@",str);
                [imageArray addObject:data];
            }
            
    
                
        }
    }
        
    UIImage *image = [[YYFrameImage alloc] initWithImageDataArray:imageArray oneFrameDuration:0.1 loopCount:0];
    // 显示多图
    self.animatedImageView = [[YYAnimatedImageView alloc] initWithImage:image];
    self.animatedImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.animatedImageView.hidden = YES;
    
    self.staticStateImageView = [[UIImageView alloc] initWithImage:BNDImageNamed(@"loading1")];
    self.staticStateImageView.contentMode = UIViewContentModeScaleAspectFit;

    if(_lineType == LKSequenceAnimationSmall)
    {

        [baseView addSubview:self.animatedImageView];
        [baseView addSubview:_staticStateImageView];
        self.animatedImageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
        _staticStateImageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
        
        [_animatedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(baseView);
            make.height.width.mas_equalTo(imageSize.width);
        }];
        
        [_staticStateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(baseView);
            make.height.width.mas_equalTo(imageSize.width);
        }];
    }
    else
    {
        UIView * newView = [[UIView alloc] init];
        [baseView addSubview:newView];
        newView.backgroundColor = [UIColor clearColor];
        //[UIColor colorWithRed:((float)((0xFAFAFA & 0xFF0000) >> 16))/255.0f green:((float)((0xFAFAFA & 0xFF00) >> 8))/255.0f blue:((float)(0xFAFAFA & 0xFF))/255.0f alpha:1];
 
        [newView addSubview:self.animatedImageView];
        [newView addSubview:_staticStateImageView];
        [newView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        [_animatedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(newView);
            make.height.width.mas_equalTo(imageSize.width);
        }];
        
        [_staticStateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(baseView);
            make.height.width.mas_equalTo(imageSize.width);
        }];
    }
}

- (void)startAnimation
{
    self.animatedImageView.hidden = NO;
    self.staticStateImageView.hidden = YES;
    self.animatedImageView.currentAnimatedImageIndex = 0;
    [self.animatedImageView startAnimating];
}

- (void)startAnimationHaveBeginAnimation
{
    [self startAnimation];
}

- (void)stopAnimation
{
    self.animatedImageView.hidden = YES;
    self.staticStateImageView.hidden = NO;
    self.animatedImageView.currentAnimatedImageIndex = 31;
    [self.animatedImageView stopAnimating];
}

@end
